<template>
  <div
    ref="custom-table"
    class="custom-table-container"
    :class="{ 'vab-fullscreen': isFullscreen }"
  >
    <vab-query-form>
      <vab-query-form-top-panel>
        <el-form
          ref="form"
          :inline="true"
          label-width="80px"
          :model="queryForm"
          @submit.native.prevent
        >
          <el-form-item
            label="停车时长"
            label-width="70px"
            prop="condition.timeUnitNum"
          >
            <el-input
              v-model="queryForm.condition.timeUnitNum"
              clearable
              placeholder="请输入停车时长"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-search"
              native-type="submit"
              type="primary"
              @click="handleQuery"
            >
              查询
            </el-button>
            <el-button icon="el-icon-refresh-left" @click="resetQuery">
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
      <vab-query-form-left-panel :lg="8" :md="8" :sm="12" :xl="8" :xs="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          添加
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :lg="16" :md="16" :sm="12" :xl="16" :xs="12">
        <el-button
          style="margin: 0 10px 10px 0 !important"
          type="primary"
          @click="clickFullScreen"
        >
          <vab-icon
            :icon="isFullscreen ? 'fullscreen-exit-fill' : 'fullscreen-fill'"
          />
          表格全屏
        </el-button>
        <el-popover
          ref="popover"
          popper-class="custom-table-checkbox"
          trigger="hover"
        >
          <el-radio-group v-model="lineHeight">
            <el-radio label="medium">大</el-radio>
            <el-radio label="small">中</el-radio>
            <el-radio label="mini">小</el-radio>
          </el-radio-group>
          <template #reference>
            <el-button style="margin: 0 10px 10px 0 !important" type="primary">
              <vab-icon icon="line-height" />
              表格尺寸
            </el-button>
          </template>
        </el-popover>
        <el-popover popper-class="custom-table-checkbox" trigger="hover">
          <el-checkbox-group v-model="checkList">
            <vab-draggable v-bind="dragOptions" :list="columns">
              <div v-for="(item, index) in columns" :key="item + index">
                <vab-icon icon="drag-drop-line" />
                <el-checkbox
                  :disabled="item.disableCheck === true"
                  :label="item.label"
                >
                  {{ item.label }}
                </el-checkbox>
              </div>
            </vab-draggable>
          </el-checkbox-group>
          <template #reference>
            <el-button
              icon="el-icon-setting"
              style="margin: 0 0 10px 0 !important"
              type="primary"
            >
              可拖拽列设置
            </el-button>
          </template>
        </el-popover>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :border="border"
      :data="list"
      :size="lineHeight"
      :stripe="stripe"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        label="序号"
        show-overflow-tooltip
        width="95"
      >
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item, index) in finallyColumns"
        :key="index"
        align="center"
        :label="item.label"
        :width="item.width"
      >
        <template #default="{ row }">
          <template v-if="item.prop === 'areaNo'">
            <span>{{ cellCode[row[item.prop]] || '-' }}</span>
          </template>
          <template v-else-if="item.prop === 'timeUnit'">
            <span>{{ timeUnitJson[row[item.prop]] || '-' }}</span>
          </template>
          <span v-else>{{ row[item.prop] || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="85">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <park-fee-edit ref="parkFeeRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, doDelete } from '@/api/park'
  import ParkFeeEdit from '../components/ParkFeeEdit'
  import { picBaseUrl } from '@/config'
  import VabDraggable from 'vuedraggable'
  import { mapGetters } from 'vuex'

  export default {
    name: 'ParkFee',
    components: {
      ParkFeeEdit,
      VabDraggable,
    },
    data() {
      return {
        baseUrl: picBaseUrl,
        isFullscreen: false,
        border: true,
        fold: true,
        height: this.$baseTableHeight(1),
        stripe: false,
        timeUnitJson: { 0: '分钟', 1: '小时' },
        lineHeight: 'medium',
        checkList: ['停车时长', '时间单位', '价位', '小区名称', '创建时间'],
        columns: [
          {
            label: '停车时长',
            width: 'auto',
            prop: 'timeUnitNum',
            disableCheck: true,
          },
          {
            label: '时间单位',
            width: 'auto',
            prop: 'timeUnit',
          },
          {
            label: '价位',
            width: 'auto',
            prop: 'price',
          },
          {
            label: '小区名称',
            width: 'auto',
            prop: 'areaNo',
          },
          {
            label: '创建时间',
            width: '200',
            prop: 'createTime',
          },
        ],
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          condition: {
            timeUnitNum: '',
            orderBy: 'id',
            orderDirect: 'desc',
          },
          pageNum: 1,
          pageSize: 10,
          queryDate: [],
        },
      }
    },
    computed: {
      ...mapGetters({
        cellCode: 'dict/cellCodeJson',
      }),
      dragOptions() {
        return {
          animation: 600,
          group: 'description',
        }
      },
      finallyColumns() {
        return this.columns.filter((item) =>
          this.checkList.includes(item.label)
        )
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      handleEdit(row) {
        console.log(row)
        if (row.id) {
          this.$refs['parkFeeRef'].showEdit(row)
        } else {
          this.$refs['parkFeeRef'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            try {
              const res = await doDelete(row.id)
              if (res && res.status === 0) {
                this.$baseMessage(
                  '删除成功！',
                  'success',
                  'vab-hey-message-success'
                )
                await this.fetchData()
              } else {
                this.$baseMessage(
                  res ? res.msg : '删除失败！',
                  'error',
                  'vab-hey-message-error'
                )
              }
            } catch (e) {
              console.log(e)
              this.$baseMessage(
                e ? e.message : '删除失败！',
                'error',
                'vab-hey-message-error'
              )
            }
          })
        } else {
          this.$baseMessage(
            '选择项id不存在！',
            'error',
            'vab-hey-message-error'
          )
        }
      },
      clickFullScreen() {
        this.isFullscreen = !this.isFullscreen
        this.handleHeight()
      },
      handleFold() {
        this.fold = !this.fold
        this.handleHeight()
      },
      handleHeight() {
        if (this.isFullscreen) this.height = this.$baseTableHeight(1) + 210
        else this.height = this.$baseTableHeight(1)
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        try {
          const param = {
            pageNum: this.queryForm.pageNum,
            pageSize: this.queryForm.pageSize,
            condition: {},
          }
          Object.keys(this.queryForm.condition).forEach((k) => {
            if (this.queryForm.condition[k]) {
              param.condition[k] = this.queryForm.condition[k]
            }
          })
          const res = await getList(this.queryForm)
          if (res && res.data) {
            const { list, total } = res.data
            this.list = list || []
            this.total = total
          }
        } catch (e) {
          console.log(e)
        }
        this.listLoading = false
      },
      resetQuery() {
        this.$refs['form'].resetFields()
        this.queryForm.pageNum = 1
        this.fetchData()
      },
    },
  }
</script>

<style lang="scss" scoped>
  .custom-table-container {
    ::v-deep {
      i {
        cursor: pointer;
      }
    }

    .stripe-panel,
    .border-panel {
      margin: 0 10px $base-margin/2 10px !important;
    }
  }
</style>
<style lang="scss">
  .custom-table-checkbox {
    [class*='ri'] {
      vertical-align: -2.5px;
      cursor: pointer;
    }

    .el-checkbox {
      margin: 5px 0 5px 8px;
    }
  }
</style>
